package com.example.medicalzb.service;

import com.example.medicalzb.entity.AccountEntity;
import com.example.medicalzb.entity.DoctorEntity;
import com.example.medicalzb.mapper.AccountMapper;
import com.example.medicalzb.mapper.DoctorMapper;
import com.example.medicalzb.model.DoctorLevelModel;
import com.example.medicalzb.model.DoctorModel;
import com.example.medicalzb.model.TreatTypeModel;
import com.example.medicalzb.param.DoctorParam;
import com.example.medicalzb.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private AccountMapper accountMapper;
    public Msg getDoctorWithPage(DoctorParam doctorParam) {
        if (doctorParam.getSize() == 0){
            doctorParam.setSize(1);
        }
        PageHelper.startPage(doctorParam.getPn(), doctorParam.getSize());
        List<DoctorModel> list = doctorMapper.getAllDoctor(doctorParam);
        PageInfo<DoctorModel> pageInfo = new PageInfo<DoctorModel>(list,5);
        return Msg.success().data("doctorInfo",pageInfo);
    }

    public Msg getLevelAndType() {
        List<TreatTypeModel> treatTypeModels = doctorMapper.getAllTreatType();
        List<DoctorLevelModel> doctorLevelModels = doctorMapper.getAllLevel();
        return Msg.success().data("allTreatType",treatTypeModels).data("allLevel",doctorLevelModels);
    }

    public Msg saveDoctor(DoctorParam doctorParam) {
        int i,j;
        AccountEntity accountEntity = new AccountEntity();
        accountEntity.setCreatetime(new DateTime().toDate());
        accountEntity.setUpdatetime(new DateTime().toDate());
        accountEntity.setUname(doctorParam.getName() + doctorParam.getPhoneNumber().substring(7));//用户名为姓名加手机尾号
        accountEntity.setPwd(new BCryptPasswordEncoder().encode(doctorParam.getPwd()));
        accountEntity.setPhoneNumber(doctorParam.getPhoneNumber());
        accountEntity.setRealname(doctorParam.getName());
        accountEntity.setUtype("ROLE_2");

        int checkPhone = accountMapper.checkPhone(doctorParam.getPhoneNumber());
        if (checkPhone > 0) {
            return Msg.fail().code(10001).mess("手机号已被使用");
        }try {
            i = accountMapper.regist(accountEntity);
        }catch (DuplicateKeyException e){
            return Msg.fail().mess("账号已被注册");
        }

        DoctorEntity doctorEntity = new DoctorEntity();
        BeanUtils.copyProperties(doctorParam,doctorEntity);
        doctorEntity.setCreateTime(new DateTime().toDate());
        doctorEntity.setUpdateTime(new DateTime().toDate());
        doctorEntity.setAccountId(accountEntity.getId());

        j = doctorMapper.saveDoctor(doctorEntity);
        if (i > 0 && j > 0){
            List<DoctorModel> list = doctorMapper.getAllDoctor(null);
            doctorEntity.setTotal((long) list.size());
            doctorParam.setPwd("");
            Long num = doctorEntity.getTotal() % 5 == 0?(doctorEntity.getTotal()/5) : (doctorEntity.getTotal()/5)+1;
            return Msg.success().mess("添加医师成功").data("pages",num).data("addData",doctorParam);
        }
        return Msg.fail().mess("添加医师失败");
    }

    public Msg updateDoctor(Long id, DoctorParam doctorParam) {
        DoctorModel doctorModel = doctorMapper.getDoctorById(id);
        //System.out.println(doctorModel.getPhoneNumber());
        int num = accountMapper.checkPhone(doctorParam.getPhoneNumber());
        if (num > 0 && !(doctorModel.getPhoneNumber().equals(doctorParam.getPhoneNumber()))){
            return Msg.fail().code(10001).mess("手机号已经被占用");
        }
        AccountEntity accountEntity = new AccountEntity();
        accountEntity.setId(doctorParam.getAccountId());
        //System.out.println(accountEntity.getId());
        accountEntity.setUname(doctorParam.getName() + doctorParam.getPhoneNumber().substring(7));
        accountEntity.setPhoneNumber(doctorParam.getPhoneNumber());
        accountEntity.setUpdatetime(new DateTime().toDate());
        accountEntity.setRealname(doctorParam.getName());
        int i = accountMapper.updateAccount(accountEntity);
        DoctorEntity doctorEntity = new DoctorEntity();
        BeanUtils.copyProperties(doctorParam, doctorEntity);
        doctorEntity.setId(id);
        doctorEntity.setUpdateTime(new DateTime().toDate());
        int j = doctorMapper.updateDoctor(doctorEntity);

        if (i > 0 && j > 0){
            doctorParam.setPwd("");
            return Msg.success().mess("修改医师信息成功").data("updateData",doctorParam);
        }
        return Msg.fail().mess("修改医师信息失败");
    }

    public Msg deleteDoctor(Long id) {
        int j = accountMapper.deleteAccount(id);
        int i = doctorMapper.deleteDoctor(id);
        if (i > 0 && j > 0){
            return Msg.success().mess("删除医师成功").data("deleteDocInfo",1).data("deleteDocAccount",1);
        }
        return Msg.fail().mess("删除医师失败");
    }

    public Msg resetPwd(Long id, String pwd) {
        String newPwd = new BCryptPasswordEncoder().encode(pwd);
        int i = accountMapper.resetPwd(id, newPwd);
        if (i > 0){
            return Msg.success().mess("重置密码成功");
        }
        return Msg.fail().mess("重置密码失败");
    }
}
